package com.gz.jedis;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Jedis_ {

    @Test
    //连接Redis
    public void con(){
        Jedis jedis = new Jedis("192.168.131.135", 6379);
        //如果设置了密码，那要jedis.auth("foobared");
        jedis.auth("foobared");
        String pong = jedis.ping();
        System.out.println(pong);
        jedis.close(); //关闭连接，并没有关闭redis
    }

    @Test
    public void StringRedis(){
        Jedis jedis = new Jedis("192.168.131.135", 6379);
        jedis.auth("foobared");
//        jedis.set("k1", "jack");
//        String k1 = jedis.get("k1");
//        System.out.println(k1);

        //批量设置k-v
        jedis.mset("k2", "tom", "k3", "jerry");
        List<String> mget = jedis.mget("k1", "k2", "k3");
        for (int i = 0; i < mget.size(); i++) {
            System.out.println("k"+i+"="+mget.get(i));
        }
        jedis.close();
    }

    @Test
    public void list(){
        Jedis jedis = new Jedis("192.168.131.135", 6379);
        jedis.auth("foobared");
        //添加list数据 一个key可以有多个值,底层双向链表
        jedis.lpush("name_list", "value1", "value2", "value3");
        List<String> nameList = jedis.lrange("name_list", 0, -1);
        for (String name : nameList) {
            System.out.println("name->"+name);
        }
        jedis.close();
    }

    //set
    @Test
    public void set() {

        Jedis jedis = new Jedis("192.168.131.135", 6379);
        jedis.auth("foobared");

        jedis.sadd("city", "北京", "上海");
        jedis.sadd("city", "广州");
        jedis.sadd("city", "深圳"); //中文在redis中一hashcode存放

        Set<String> smembers = jedis.smembers("city");
        for (String city : smembers) {
            System.out.println("city-->" + city);
        }

        jedis.close();
    }

    //hash
    @Test
    public void hash() {

        Jedis jedis = new Jedis("192.168.131.135", 6379);
        jedis.auth("foobared");

        //设置
        jedis.hset("hash01", "name", "李白");
        jedis.hset("hash01", "age", "30");
        //k - 域 - 值

        //获取
        String name = jedis.hget("hash01", "name");
        System.out.println("name=>" + name);

        //先构建一个map,然后加入
        Map<String, String> maps = new HashMap<String, String>();
        maps.put("job", "java工程师");
        maps.put("name", "smith");
        maps.put("email", "smith@qq.com");
        jedis.hset("hash02", maps);

        //取出看看
        List<String> person =
                jedis.hmget("hash02", "job", "name", "email");
        for (String s : person) {
            System.out.println("s===>" + s);
        }


        jedis.close();

    }

    //zset-有序集合
    @Test
    public void zset() {

        Jedis jedis = new Jedis("192.168.131.135", 6379);
        jedis.auth("foobared");

        //添加
        jedis.zadd("hero", 1, "关羽");
        jedis.zadd("hero", 2, "张飞");
        jedis.zadd("hero", 5, "黄忠");
        jedis.zadd("hero", 3, "赵云");
        jedis.zadd("hero", 4, "马超");
        //k - score - member

        //取出
        List<String> heroes = jedis.zrange("hero", 0, -1);//按照评分从小到大
//Set<String> heroes = jedis.zrevrange("hero", 0, -1);//按照评分从大到小
        for (String hero : heroes) {
            System.out.println("hero= " + hero);
        }
        jedis.close();
    }
}
